# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid

from components.models import KubernetesClusterSign, KubernetesManage
from utils.mysql_utils import query_to_list, sql_decrypt
from utils.views import ReturnClass


def get_kubernetes_cluster_sign_info(kubernetes_cluster_id):
    """
    查询API主机的信息
    @param host_id:
    @param kubernetes_cluster_id:
    @return:
    """
    sql = """
    SELECT b.host_id, b.host_ip,b.host_connection_port,
    b.host_connection_code,c.resource_user_name,c.resource_user_pass,c.resource_user_keyfile,b.host_nick,
    d.kubernetes_runtime
    FROM kubernetes_cluster_sign a
    LEFT JOIN hosts_management b ON a.host_id = b.host_id
    LEFT JOIN resource_user c ON b.resource_user_ids = c.resource_user_ids AND c.resource_user_default = 1
    LEFT JOIN kubernetes_cluster d ON a.kubernetes_cluster_id = d.kubernetes_cluster_id
    WHERE a.kubernetes_cluster_id = {kubernetes_cluster_id}
    """.format(
        kubernetes_cluster_id=kubernetes_cluster_id
    )

    kubernetes_cluster_host_data = query_to_list(sql, [], to_one_dict=True)

    if kubernetes_cluster_host_data['resource_user_pass']:
        kubernetes_cluster_host_data['resource_user_pass'] = sql_decrypt(kubernetes_cluster_host_data['resource_user_pass'])
    else:
        return ReturnClass.return_bust("主机密码维护为空，或未指定默认用户")

    return ReturnClass.return_succeed(kubernetes_cluster_host_data)


def quit_k8s_download_cmd(runtime):
    sql = """
    SELECT * FROM system_function a 
    WHERE a.system_function_group = "download_tar_image"
    AND a.system_version = '{runtime}'
    ORDER BY a.order_by DESC
    """.format(runtime=runtime)

    data = query_to_list(sql, [])

    return data